# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import fields, models, api

import logging
_logger = logging.getLogger(__name__)


class ResPartner(models.Model):
    _inherit = "res.partner"


class ResUsers(models.Model):
    _inherit = "res.users"

    def create_company(self, name):
        comp = self.env['res.company']
        comp = comp.search([('name', '=', name)])
        if comp:
            return {'error': {'message': 'company name: %s is exist' % name}}

        comp = comp.create({'name': name})
        chart_template_id = self.env.ref(
            'l10n_cn_small_business.l10n_chart_china_small_business')

        conf = self.sudo().with_context(
            allowed_company_ids=comp.ids).env['res.config.settings']
        conf = conf.create({'chart_template_id': chart_template_id.id})
        conf.execute()
        conf = conf.create({'sale_tax_id': None, 'purchase_tax_id': None})
        conf.execute()

        return {'result': True}
